limsBatchRevise.detailPage = {

    getBtnToolbar : function() {
        var _this = this;
        if (_this.param.audit === '1'){
            return {
                type : 'btnToolbar',
                items : [{
                    type : 'button',
                    text : 'GIKAM.BUTTON.BACK',
                    icon : 'back',
                    onClick : function() {
                        workspace.window.goBack();
                    }
                }, /*{
                    type : 'button',
                    text : 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon : 'pass',
                    onClick : function() {
                        var submitUrl = Gikam.printf(limsBatchRevise.baseUrl + '/approve/{id}', {
                            id: _this.param.id
                        })
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.TIP.SUBMIT', function () {
                            workspace.window.showMask()
                            Gikam.postText(submitUrl, Gikam.getJsonWrapper(null, [null, null])).done(function () {
                                workspace.window.goBack();
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                }, {
                    type : 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick : function() {
                        var rejectUrl = Gikam.printf(limsBatchRevise.baseUrl + '/reject/{id}', {
                            id: _this.param.id
                        })
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.TIP.SUBMIT', function () {
                            workspace.window.showMask()
                            Gikam.postText(rejectUrl, Gikam.getJsonWrapper(null, [null, null])).done(function () {
                                workspace.window.goBack();
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                }*/{// 换成工作流的
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon: 'pass',
                    onClick: function () {
                        _this.passSingle();
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick: function () {
                        _this.rejectSingle()
                    }
                }]
            }
        }
        if (_this.param.readonly === '1'){
            return {
                type : 'btnToolbar',
                items : [{
                    type : 'button',
                    text : 'GIKAM.BUTTON.BACK',
                    icon : 'back',
                    onClick : function() {
                        workspace.window.goBack();
                    }
                }]
            }
        }
        return {
            type : 'btnToolbar',
            items : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.BACK',
                icon : 'back',
                onClick : function() {
                    workspace.window.goBack();
                }
            }, /*{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var data = Gikam.getComp('batch-sample-list-grid').getData();
                    if (Gikam.isEmpty(data)) {
                        Gikam.alert('BATCH.SUBBMIT.CHECK_SAMPLE_NOTEMPTY');
                        return;
                    }
                    var submitUrl = Gikam.printf(limsBatchRevise.baseUrl + '/submitById/{id}', {
                        id: _this.param.id
                    })
                    Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                        workspace.window.showMask()
                        Gikam.postText(submitUrl, Gikam.getJsonWrapper(null, [null, null])).done(function () {
                            workspace.window.goBack();
                        }).always(function () {
                            workspace.window.closeMask()
                        })
                    })
                }
            }*/{// 换成工作流
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick : function() {
                    _this.submitSingle();
                }
            }, ]
        }
    },

    submitSingle: function () {
        let _this = this
        Gikam.create('workflow').submit({
            data: [{id: _this.param.id}],
            pageObject : limsBatchRevise,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    passSingle: function () {
        let _this = this
        Gikam.create('workflow').pass({
            data: [{id: _this.param.id}],
            pageObject : limsBatchRevise,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    rejectSingle: function () {
        let _this = this
        Gikam.create('workflow').reject({
            data: [{id: _this.param.id}],
            pageObject : limsBatchRevise,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    getBaseInfoTab : function() {
        var _this = this;
        var url = limsBatchRevise.batchSampleTempUrl + '/select/samples';
        var requestData = {
            reviseId_EQ : Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
        };
        var table = 'T_LIMS_BATCH_REVISE';
        var bizId = Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
        if(_this.param.status === 'done'||_this.param.processStatus==='done') {  // 完成状态上下做个调换
            url = limsBatch.batchSampleUrl + '/select/samples';
            requestData = {
                batchId_EQ : Gikam.isEmpty(_this.param.batchId) ? '-1' : _this.param.batchId
            };
            table = 'T_LIMS_BATCH';
            bizId = Gikam.isEmpty(_this.param.batchId) ? '-1' : _this.param.batchId
        }
        return {
            onActived : function(data) {
                var tab1=Gikam.getComp('lims-batch-revise-detail-tab');
                var tab2=Gikam.getComp('lims-batch-revise-detail-tab-old');
                switch (data.index) {
                    case 0:
                        tab1.showPanel('batch-sample-list-grid-panel');
                        tab2.showPanel('batch-sample-list-grid-old-panel');
                        break;
                    case 1:
                        tab1.showPanel('logistics-info-grid-panel');
                        tab2.showPanel('logistics-info-grid-old-panel');
                        break;
                    case 2:
                        tab1.showPanel('lims-batch-special-uploader-panel');
                        tab2.showPanel('lims-batch-special-uploader-old-panel');
                        break;
                    default:
                }
            },
            type : 'tab',
            id : 'lims-batch-revise-detail-tab',
            panels : [ {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                id : 'batch-sample-list-grid-panel',
                items : [ {
                    type : 'grid',
                    id : 'batch-sample-list-grid',
                    url : url,
                    requestData : requestData,
                    columns : limsBatchRevise.getSampleListTempColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editSampleDetail(row, false);
                    },
                    deleteFormatter: function (row) {
                        return row.sampleCode || row.id
                    },
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            _this.markSampleInfo(rows);
                        }
                    },
                    rowStyleFormatter: function (row) {
                        var rowStyle = {};
                        if(_this.param.status !== 'done') {
                            if (Gikam.isEmpty(row.sampleId)) {
                                rowStyle.backgroundColor = 'green'
                            }
                        }
                        return rowStyle;
                    },
                    onRendered: function () {
                        if(_this.param.readonly === '1') {
                            this.hideToolbar();
                        }
                        if(_this.param.reviseType === '1' || _this.param.reviseType === '2') {
                            this.hideToolbar();
                        }
                    },
                    toolbar : _this.getSampleToolbar()
                } ]
            }, {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_LOGISTICS_INFO',
                id : 'logistics-info-grid-panel',
                items : [ {
                    type : 'grid',
                    url : url,
                    requestData : requestData,
                    service : 'limsServiceTempImpl',
                    id : 'logistics-info-grid',
                    columns : limsBatchRevise.getLogisticsListTempColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editLogisticsDetail(row, false);
                    },
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            _this.markLogistics(rows);
                        }
                    },
                    onRendered: function () {
                        if(_this.param.readonly === '1') {
                            this.hideToolbar();
                        }
                        // if(_this.param.reviseType === '1' || _this.param.reviseType === '2') {
                        //     this.hideToolbar();
                        // }
                    },
                    toolbar : [ {
                        type : 'button',
                        text : 'GIKAM.BUTTON.EDIT',
                        icon : 'edit',
                        onClick : function() {
                            let selections = Gikam.getComp('logistics-info-grid').getSelections();
                            if (Gikam.isEmpty(selections)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                                return;
                            }
                            Gikam.createLimsModal({
                                title: 'GIKAM.BUTTON.EDIT',
                                width : 600,
                                height : 300,
                                onConfirm: function () {
                                    var form = Gikam.getComp('sample-logistics-form');
                                    if (form.validate()) {
                                        var data = form.getData();
                                        var chooseData = selections.map(e => {
                                            return {
                                                id : e.id,
                                                temperatureLimits : data.temperatureLimits,
                                                senderName : data.senderName,
                                                senderPhone : data.senderPhone,
                                                courierNumber : data.courierNumber,
                                                courier : data.courier,
                                                senderRemark : data.senderRemark,
                                            }
                                        })
                                        Gikam.put( limsBatchRevise.batchSampleTempUrl + '/instant', Gikam.getJsonWrapper(null, [null, chooseData])).done(function () {
                                            Gikam.getComp('logistics-info-grid').refresh()
                                        })
                                        Gikam.getLastModal().close(data);
                                    }
                                },
                                m: {
                                    type: 'form',
                                    id: 'sample-logistics-form',
                                    columns : 2,
                                    titleWidth : 100,
                                    fields: limsBatch.getSampleLogisticsFormFields(),
                                },
                            })
                        }
                    }]
                } ],
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                id: 'lims-batch-special-uploader-panel',
                items: [{
                    type: 'uploader',
                    id: 'lims-batch-special-uploader',
                    dbTable: table,
                    bizId: bizId,
                    readonly : _this.param.readonly === '1'
                }]
            }]
        }
    },

    markSampleInfo : function(rows) {
        var _this = this;
        let oldRows = Gikam.getComp('batch-sample-list-grid-old').getData();
        for (let i = 0; i < rows.length; i++) {
            let row = rows[i];
            let filter = oldRows.filter(item => item.id === row.sampleId);
            if(_this.param.status === 'done'||_this.param.processStatus==='done') {
                filter = oldRows.filter(item => item.sampleId === row.id);
            }
            if (Gikam.isNotEmpty(filter)) {
                let filterElement = filter[0];
                console.log(row);
                console.log(filterElement);
                let updateKeys = [];
                for(var key in row) {
                    if(row[key] !== filterElement[key]) {
                        console.log(key  + '  ' + row[key] + ' -- ' + filterElement[key]);
                        updateKeys.push(key);
                    }
                }
                if(row.ext$.productnames !== filterElement.ext$.productnames) {
                    updateKeys.push('ext$.productnames');
                }
                console.log(updateKeys);
                Gikam.getComp('batch-sample-list-grid').setCellStyle(i, {backgroundColor:"green"} , updateKeys);
            }
        }
    },

    markLogistics : function(rows) {
        var _this = this;
        let oldRows = Gikam.getComp('logistics-info-grid-old').getData();
        for (let i = 0; i < rows.length; i++) {
            let row = rows[i];
            let filter = oldRows.filter(item => item.id === row.sampleId);
            if(_this.param.status === 'done'||_this.param.processStatus==='done') {
                filter = oldRows.filter(item => item.sampleId === row.id);
            }
            if (Gikam.isNotEmpty(filter)) {
                let filterElement = filter[0];
                let updateKeys = [];
                for(var key in row) {
                    if(row[key] !== filterElement[key]) {
                        updateKeys.push(key);
                    }
                }
                Gikam.getComp('logistics-info-grid').setCellStyle(i, {backgroundColor:"green"} , updateKeys);
            }
        }
    },

    editSampleDetail : function (row, readonly) {
        var _this = this;
        var model = Gikam.create('modal', {
            title: 'GIKAM.BUTTON.EDIT',
            width : 600,
            height : 450,
        });
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            center: {
                items: [{
                    type: 'form',
                    id: 'batch-sample-code-form',
                    columns : 2,
                    titleWidth : 110,
                    fields: limsBatch.getSampleFormFields(),
                    readonly: readonly,
                    autoSave: false,
                    onRendered: function () {
                        this.hideFields(['sampleCode']);
                        row.productNames = row.ext$.productnames;
                        this.setData(row)
                        if(_this.param.reviseType === '1') {
                            this.toFieldsReadonly(['sampleType', 'productNames'])
                        }
                        if(_this.param.reviseType === '3' && Gikam.isNotEmpty(row.sampleId)) {
                            this.setReadonly(true)
                        }
                        if(_this.param.readonly === '1') {
                            this.setReadonly(true);
                        }
                    }
                }, {
                    type: 'btnToolbar',
                    items: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.CONFIRM',
                        class: 'blue',
                        icon: 'save',
                        color: 'white',
                        hidden: readonly || (_this.param.readonly && _this.param.readonly === '1'),
                        onClick: function () {
                            if(readonly) {
                                Gikam.getLastModal().close();
                                return;
                            }
                            var form = Gikam.getComp('batch-sample-code-form');
                            if (form.validate()) {
                                var data = form.getData();
                                Gikam.put(limsBatchRevise.batchSampleTempUrl + '/update/sample', Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                    Gikam.getComp('batch-sample-list-grid').refresh()
                                })
                                Gikam.getLastModal().close(data);
                            }
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.CANCEL',
                        icon: 'back',
                        hidden: readonly || (_this.param.readonly && _this.param.readonly === '1'),
                        onClick: function () {
                            Gikam.getLastModal().close();
                        }
                    }]
                }]
            }
        })
    },

    editLogisticsDetail: function (row, readonly) {
        var _this = this;
        Gikam.createLimsModal({
            title: 'GIKAM.BUTTON.EDIT',
            width : 600,
            height : 300,
            hideButton: readonly || (_this.param.readonly && _this.param.readonly === '1'),
            onConfirm: function () {
                if(readonly) {
                    Gikam.getLastModal().close();
                    return;
                }
                if(_this.param.readonly === '1') {
                    Gikam.getLastModal().close();
                    return;
                }
                var form = Gikam.getComp('sample-logistics-form');
                if (form.validate()) {
                    var data = form.getData();
                    var chooseData = {
                        id : row.id,
                        temperatureLimits : data.temperatureLimits,
                        senderName : data.senderName,
                        senderPhone : data.senderPhone,
                        courierNumber : data.courierNumber,
                        courier : data.courier,
                        senderRemark : data.senderRemark,
                    }
                    Gikam.put(limsBatchRevise.sampleTempUrl + '/instant', Gikam.getJsonWrapper(null, [null, [chooseData]])).done(function () {
                        Gikam.getComp('logistics-info-grid').refresh()
                    })
                    Gikam.getLastModal().close(data);
                }
            },
            m: {
                type: 'form',
                id: 'sample-logistics-form',
                columns : 2,
                titleWidth : 100,
                readonly: readonly,
                fields: limsBatch.getSampleLogisticsFormFields(),
                onRendered: function () {
                    this.setData(row);
                    if(_this.param.readonly === '1') {
                        this.setReadonly(true);
                    }
                }
            },
        })
    },

    getSampleToolbar : function () {
        var _this = this;
        return [ {
            type : 'button',
            text : 'GIKAM.BUTTON.INSERT',
            icon : 'add',
            onClick : function() {
                Gikam.preInsert({
                    modalTitle : 'GIKAM.BUTTON.INSERT',
                    fields : limsBatch.getSampleFormFields(),
                    url : limsBatchRevise.batchSampleTempUrl + '/insert/sample',
                    columns : 2,
                    titleWidth : 110,
                    width : 600,
                    height : 450,
                    disableNotEmptyValidate: true,
                    param : {
                        reviseId : _this.param.id,
                        batchId : _this.param.batchId
                    }
                }).done(function(id) {
                    Gikam.getComp('batch-sample-list-grid').refresh();
                    Gikam.getComp('logistics-info-grid').refresh();
                });
            }
        }, {
            type : 'button',
            text : 'GIKAM.BUTTON.DELETE',
            icon : 'remove-row',
            onClick : function() {
                let selections = Gikam.getComp('batch-sample-list-grid').getSelections();
                if (Gikam.isEmpty(selections)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                    return;
                }
                for (let i = 0; i < selections.length; i++) {
                    if(Gikam.isNotEmpty(selections[i].sampleId)) {
                        Gikam.alert('新增样本才能删除！');
                        return;
                    }
                }
                Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.CONFIRM_TO_DELETE', function () {
                    Gikam.del(limsBatchRevise.batchSampleTempUrl + '/delete/samples', Gikam.getJsonWrapper(null, [null, selections])).done(function (req) {
                        Gikam.getComp('batch-sample-list-grid').refresh();
                        Gikam.getComp('logistics-info-grid').refresh();
                    })
                })
            }
        }];
    },

    getOldBaseInfoTab : function() {
        var _this = this;
        var url = limsBatch.batchSampleUrl + '/select/samples';
        var requestData = {
            batchId_EQ : Gikam.isEmpty(_this.param.batchId) ? '-1' : _this.param.batchId
        };
        var table = 'T_LIMS_BATCH';
        var bizId = Gikam.isEmpty(_this.param.batchId) ? '-1' : _this.param.batchId
        if(_this.param.status === 'done'||_this.param.processStatus==='done') { // 完成状态上下做个调换
            url = limsBatchRevise.batchSampleTempUrl + '/select/samples';
            requestData = {
                reviseId_EQ : Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
            };
            table = 'T_LIMS_BATCH_REVISE';
            bizId = Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
        }
        return {
            onActived : function(data) {
                var tab1=Gikam.getComp('lims-batch-revise-detail-tab');
                var tab2=Gikam.getComp('lims-batch-revise-detail-tab-old');
                switch (data.index) {
                    case 0:
                        tab1.showPanel('batch-sample-list-grid-panel');
                        tab2.showPanel('batch-sample-list-grid-old-panel');
                        break;
                    case 1:
                        tab1.showPanel('logistics-info-grid-panel');
                        tab2.showPanel('logistics-info-grid-old-panel');
                        break;
                    case 2:
                        tab1.showPanel('lims-batch-special-uploader-panel');
                        tab2.showPanel('lims-batch-special-uploader-old-panel');
                        break;
                    default:
                }
            },
            type : 'tab',
            id : 'lims-batch-revise-detail-tab-old',
            panels : [ {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                id : 'batch-sample-list-grid-old-panel',
                items : [ {
                    type : 'grid',
                    id : 'batch-sample-list-grid-old',
                    url : url,
                    requestData : requestData,
                    columns : limsBatchRevise.getSampleListTempColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editSampleDetail(row, true);
                    },
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            let newRows = Gikam.getComp('batch-sample-list-grid').getData();
                            _this.markSampleInfo(newRows);
                        }
                    },
                } ]
            }, {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_LOGISTICS_INFO',
                id : 'logistics-info-grid-old-panel',
                items : [ {
                    type : 'grid',
                    url : url,
                    requestData : requestData,
                    service : 'limsServiceImpl',
                    id : 'logistics-info-grid-old',
                    columns : limsBatchRevise.getLogisticsListTempColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editLogisticsDetail(row, true);
                    },
                    onLoadSuccess: function (rows) {
                        if (Gikam.isNotEmpty(rows)) {
                            let newRows = Gikam.getComp('logistics-info-grid').getData();
                            _this.markLogistics(newRows);
                        }
                    },
                } ]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                id : 'lims-batch-special-uploader-old-panel',
                items: [{
                    type: 'uploader',
                    id: 'lims-batch-special-uploader-old',
                    dbTable: table,
                    bizId: bizId,
                    readonly : true
                }]
            }]
        }
    },

    centerParam: function() {
        var _this = this;
        return {
            type : 'layout',
            north : {
                height : '50%',
                items : [_this.getBaseInfoTab()]
            },
            center : {
                title: 'T_LIMS_BATCH.TAB.HISTORY_BATCH',
                items : [_this.getOldBaseInfoTab()]
            }
        }
    },

    createPage : function() {
        Gikam.create('layout', {
            center : {
                items : [ this.getBtnToolbar(), this.centerParam() ]
            },
            toolbarAlign: 'right',
            renderTo : workspace.window.$dom
        });
        return this;
    },

    init : function(param) {
        this.param = param;
        this.createPage();
    }
}
